/*******************************************************************************
 * Copyright (c) 2012 joey.enfield.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     joey.enfield - initial API and implementation
 ******************************************************************************/
package com.joey.software.dsp;

import com.joey.software.framesToolkit.FrameFactroy;

import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;

public class FFTAccuracyComparison
{
	public static void main(String input[])
	{
		double[] dataRaw = new double[]
		                          	{ 30.649115, 37.728386, 36.331264, 39.24357, 40.586082, 36.090168,
		                          			35.743828, 37.24815, 38.89603, 43.449856, 42.82445, 42.925858,
		                          			39.100796, 37.092617, 39.40061, 41.104584, 41.86538, 42.472343,
		                          			42.468605, 43.351627, 41.809414, 33.34186, 37.05979, 41.31965,
		                          			43.495365, 44.29211, 41.45707, 43.735806, 43.342617, 40.45246,
		                          			40.382942, 40.484596, 41.344036, 41.955677, 42.43319, 42.167294,
		                          			41.60017, 41.247448, 37.564896, 37.065468, 39.750183, 40.06379,
		                          			41.03358, 40.75909, 42.583893, 43.730522, 40.498764, 39.57931,
		                          			38.117447, 39.24462, 40.778957, 42.95973, 43.818775, 40.954414,
		                          			41.892227, 41.407825, 35.568214, 33.548306, 36.465286, 38.7022,
		                          			40.495872, 42.72074, 46.04552, 46.764545, 46.233307, 43.733242,
		                          			37.597317, 36.752277, 37.392735, 39.785755, 38.71732, 41.40918,
		                          			42.601875, 41.536278, 39.225483, 36.458725, 38.468815, 40.23353,
		                          			42.2506, 43.58121, 41.85133, 37.156666, 37.784714, 40.72888,
		                          			40.36655, 40.729282, 42.0675, 41.32117, 39.024323, 35.763138,
		                          			39.078243, 39.40577, 43.248814, 44.304142, 43.894455, 40.612537,
		                          			39.337708, 38.2184, 37.20272, 40.27586, 40.703217, 43.485653,
		                          			43.030262, 41.642097, 39.962147, 36.63367, 36.380978, 41.053856,
		                          			41.570847, 42.488815, 42.175507, 41.52457, 43.283855, 40.75922,
		                          			34.964542, 34.2659, 40.828335, 41.314, 43.24564, 43.09245,
		                          			42.275234, 39.127563, 35.18983, 36.94874, 40.978592, 41.314465,
		                          			42.714947, 41.83961, 41.627827, 39.66281, 39.73172, 37.781654,
		                          			37.11681, 41.211407, 40.750534, 41.059715, 40.30116, 38.28009,
		                          			36.537292, 39.178345, 40.524765, 41.17121, 40.978683, 40.904686,
		                          			42.60815, 38.166016, 36.447174, 37.764454, 40.287617, 40.9747,
		                          			41.185104, 42.492252, 40.809074, 39.3454, 35.184, 36.901485,
		                          			41.566082, 42.312775, 42.212357, 41.763386, 41.903046, 41.20625,
		                          			37.290405, 37.207043, 38.60172, 39.47927, 41.21336, 43.560287,
		                          			42.896015, 41.048515, 38.300533, 36.317017, 37.03569, 40.44745,
		                          			41.1794, 41.542942, 41.881092, 41.059845, 41.77285, 41.4807,
		                          			36.009842, 37.01965, 40.533855, 41.07957, 42.920925, 43.84232,
		                          			41.04621, 38.194557, 36.00043, 38.32642, 40.628162, 41.062305,
		                          			42.273514, 42.81462, 45.49531, 45.02202, 35.0925, 36.139217,
		                          			39.88427, 41.149532, 41.080402, 39.750366, 41.693855, 39.063244,
		                          			36.1975, 35.222866, 39.578686, 39.477043, 38.790768, 39.90742,
		                          			41.66142, 40.256054, 38.03948, 41.53091, 42.862656, 43.985157,
		                          			37.955715, 43.970573, 47.69164, 57.506016, 28.479206, 33.173683,
		                          			45.444347, 41.833855 };
		
		
		float real[] = new float[256];
		float imag[] = new float[256];
		float mag[] = new float[256];
		
		for(int i = 0; i < real.length; i++)
		{
			real[i] = (float)dataRaw[i%dataRaw.length];
			imag[i] = 0;
		}
		
		fft1(real,imag, mag);
		
		FrameFactroy.getFrame(mag);
	}
	
	public static void fft1(float[] real, float[] imag, float[] mag)
	{
		float[] data = new float[real.length*2];
		FloatFFT_1D fft = new FloatFFT_1D(real.length);
		
		for(int i= 0; i < real.length; i++)
		{
			data[2*i] = real[i];
			data[2*i+1] = imag[i];
		}
		
		fft.complexForward(data);
		
		for(int i = 0; i < mag.length; i++)
		{
			mag[i] = (float)Math.sqrt(data[2*i]*data[2*i]+data[2*i+1]*data[2*i+1]);
		}
	}
}
